package com.ruoyi.project.product.styleView.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.product.lineManage.domain.LineManage;
import com.ruoyi.project.product.styleView.domain.StyleView;
import com.ruoyi.project.product.styleView.service.StyleViewService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/product/styleView")
public class StyleViewController extends BaseController {
    //声明业务层属性
    @Autowired
    private StyleViewService styleViewServiceImpl;
    //声明资源跳转的公共前缀
    private String prefix = "product/styleView";
    //声明单元方法:完成产品风格页面跳转
    @RequiresPermissions("product:styleView:view")
    @GetMapping()
    public String productStyle(){
        return prefix+"/StyleView";
    }

    /**
     * 声明单元方法，完成产品风格菜单的加载
     * @param
     * @return
     */
    @RequiresPermissions("product:styleView:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StyleView styleView)
    {
        startPage();
        List<StyleView> list = styleViewServiceImpl.selectStyleViewList(styleView);
        return getDataTable(list);
    }
    /**
     * 新增单元方法:完成新增产品风格页面的转发
     * @return
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
    /**
     * 校验名称
     */
    @PostMapping("/checkStyleViewNameUnique")
    @ResponseBody
    public String checkStyleViewNameUnique(StyleView styleView)
    {
        return styleViewServiceImpl.checkStyleViewNameUnique(styleView);
    }

    /**
     * 新增保存产品系列
     */
    @RequiresPermissions("product:styleView:add")
    @Log(title = "产品风格管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated StyleView styleView)
    {
        if (UserConstants.STYLE_NAME_NOT_UNIQUE.equals(styleViewServiceImpl.checkStyleViewNameUnique(styleView)))
        {
            return error("新增产品风格'" + styleView.getStyleName() + "'失败，产品风格名称已存在");
        }
        return toAjax(styleViewServiceImpl.insertStyleView(styleView));
    }

    /**
     * 修改产品风格，跳转修改页面并完成数据的回显
     */
    @GetMapping("/edit/{styleId}")
    public String edit(@PathVariable("styleId") Long styleId, ModelMap mmap)
    {
        mmap.put("StyleView", styleViewServiceImpl.selectStyleViewById(styleId));
        return prefix + "/edit";
    }

    /**
     * 修改保存产品风格
     */
    @RequiresPermissions("product:styleView:edit")
    @Log(title = "产品风格管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated StyleView styleView)
    {
        if (UserConstants.STYLE_NAME_NOT_UNIQUE.equals(styleViewServiceImpl.checkStyleViewNameUnique(styleView)))
        {
            return error("修改产品风格类别'" + styleView.getStyleName() + "'失败，产品风格名称已存在");
        }
        return toAjax(styleViewServiceImpl.updateStyleView(styleView));
    }

    /**
     * 删除产品风格
     * @param ids
     * @return
     */
    @RequiresPermissions("product:styleView:remove")
    @Log(title = "产品风格管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(styleViewServiceImpl.deleteStyleViewByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
}
